﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Rison.DataAccess.Common;
using Rison.DBHelper;

namespace Rison.DataAccess.Data.XT
{
    public sealed class SysUserDb : DataAccessBase 
    {
        public SysUserDb()
            : base()
        {
        }

        public SysUserDb(DbBaseHelper dbTransaction)
            : base(dbTransaction)
        {
        }

        protected override string tableName
        {
            get { return "Users"; }
        }

        protected override string IdentityFieldName
        {
            get { return "UID"; }
        }

        protected override string OrderBy
        {
            get { return "UserId"; }
        }

        protected override string GetSqlFromTable()
        {
            return "select *,case when name is null or 0=len(name) then userid else name end as UName from Users";
        }

        public DataSet GetDepartmentbyUser(string userid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select * from v_department where userid='{0}' order by ordername", userid.ToString());
            
            //sql.Append("select * from v_department");

            return base.ExecuteSql(sql.ToString(), "GetDepartmentbyUser");
        }

        public DataRow GetDepartmentName(string departid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select * from department where departid='{0}' order by ordername", departid.ToString());

            //sql.Append("select * from v_department");

            return base.ExecuteSqlToDataRow(sql.ToString());
        }

        public DataRow RetriveByUser(string userid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select * from users where userid='{0}'", userid.ToString());

            //sql.Append("select * from v_department");

            return base.ExecuteSqlToDataRow(sql.ToString());
        }

    }
}
